package com.jp.knowledge.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.FrameLayout;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.PositionKnowleageItemFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.PositionKnowlegeDataLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author jiayen     2017-06-08 20:01:48
 * 岗位工具
 */
public class PositiontoolActivity extends SlidingActivity {

    private String id;
    private String cachePath;
    private FileCacheManager fileCacheManager;
    private PositionKnowleageItemFragment fragment;

    @Override
    protected int getResourceId() {
        return R.layout.activity_positiontool;
    }

    @Override
    protected void init() {
        topName.setText("岗位工具");
        rightIcon.setVisibility(View.GONE);
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        id = "c207e713-63b2-9da1-28cf-b22eb4ef8472";
        cachePath = mContext.getCacheDir() + "/skillNavList_" + id + ".dat";
        fileCacheManager = new FileCacheManager();
        fragment = new PositionKnowleageItemFragment().newInstance(null, true);
        getSupportFragmentManager().beginTransaction().add(R.id.content, fragment).commit();
        readCache();
    }

    private void getData(){
        JsonObject param = new JsonObject();
        param.addProperty("id", id);
        HttpHelper.getInstance(mContext).skillNavList(param, 0x01, new ResultSubscriber.OnResultListener() {
            @Override
            public void onStart(int requestType) {}
            @Override
            public void onCompleted(int requestType) {}
            @Override
            public void onError(int requestType) {}
            @Override
            public void onNext(IModel data, int requestType) {
                if(data.getErrcode() != 0) {
                    ToasUtil.toast(mContext, data.getMessage());
                } else{
                    List<PositionKnowlegeMode> positionKnowlegeModes = data.getList(PositionKnowlegeMode.class);
                    if(positionKnowlegeModes != null && positionKnowlegeModes.size() > 0) {
                        topName.setText(positionKnowlegeModes.get(0).getName());
                        fragment.updateData(positionKnowlegeModes.get(0));
                        saveCache(positionKnowlegeModes.get(0), cachePath);
                    }
                }
            }
        });
    }

    private void readCache(){
        Observable.create(new Observable.OnSubscribe<PositionKnowlegeMode>() {
            @Override
            public void call(Subscriber<? super PositionKnowlegeMode> subscriber) {
                PositionKnowlegeMode data = (PositionKnowlegeMode) fileCacheManager.readObject(cachePath);
                subscriber.onNext(data);
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<PositionKnowlegeMode>() {
            @Override
            public void onCompleted() {
                getData();
            }
            @Override
            public void onError(Throwable e) {
                onCompleted();
            }
            @Override
            public void onNext(PositionKnowlegeMode data) {
                if(data != null) {
                    topName.setText(data.getName());
                    fragment.updateData(data);
                }
                onCompleted();
            }
        });
    }

    private void saveCache(final Object data, final String cachePath){
        new Thread(new Runnable() {
            @Override
            public void run() {
                fileCacheManager.wirteObject(data, cachePath);
            }
        }).start();
    }
}
